package cn.iocoder.yudao.module.product.service.resourcecategory;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.resourcecategory.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.resourcecategory.ResourceCategoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.product.dal.mysql.resourcecategory.ResourceCategoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 产品资源分享分类 Service 实现类
 *
 * @author 阿杰
 */
@Service
@Validated
public class ResourceCategoryServiceImpl implements ResourceCategoryService {

    @Resource
    private ResourceCategoryMapper resourceCategoryMapper;

    @Override
    public Integer createResourceCategory(ResourceCategorySaveReqVO createReqVO) {
        // 插入
        ResourceCategoryDO resourceCategory = BeanUtils.toBean(createReqVO, ResourceCategoryDO.class);
        resourceCategoryMapper.insert(resourceCategory);
        // 返回
        return resourceCategory.getId();
    }

    @Override
    public void updateResourceCategory(ResourceCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateResourceCategoryExists(updateReqVO.getId());
        // 更新
        ResourceCategoryDO updateObj = BeanUtils.toBean(updateReqVO, ResourceCategoryDO.class);
        resourceCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteResourceCategory(Integer id) {
        // 校验存在
        validateResourceCategoryExists(id);
        // 删除
        resourceCategoryMapper.deleteById(id);
    }

    private void validateResourceCategoryExists(Integer id) {
        if (resourceCategoryMapper.selectById(id) == null) {
            throw exception(RESOURCE_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public ResourceCategoryDO getResourceCategory(Integer id) {
        return resourceCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceCategoryDO> getResourceCategoryPage(ResourceCategoryPageReqVO pageReqVO) {
        return resourceCategoryMapper.selectPage(pageReqVO);
    }

}